package classes
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	[Bindable]
	public class Settings extends EventDispatcher{
	
		private static var singleton : Settings
		
		private var _qbeServletURL:String;
		private var _qbcServletURL:String;
		private var _randomServletURL:String;
		private var _requestRSSServletURL:String;
		private var _qbnServletURL:String;
		private var _qbtServletURL:String;
		private var _getTagsServletURL:String;
		private var _taggingServletURL:String;
		private var _tagCloudServletURL:String;
		private var _getImageTagsServletURL:String;
		private var _logoURL:String;
		
		[Bindable("settingsChanged")]
		public function get qbeServletURL():String{
			return _qbeServletURL;
		}
		[Bindable("settingsChanged")]
		public function get qbcServletURL():String{
			return _qbcServletURL;
		}
		[Bindable("settingsChanged")]
		public function get randomServletURL():String{
			return _randomServletURL;
		}
		[Bindable("settingsChanged")]
		public function get requestRSSServletURL():String{
			return _requestRSSServletURL;
		}
		[Bindable("settingsChanged")]
		public function get qbnServletURL():String{
			return _qbnServletURL;
		}
		[Bindable("settingsChanged")]
		public function get qbtServletURL():String{
			return _qbtServletURL;
		}
		[Bindable("settingsChanged")]
		public function get getTagsServletURL():String{
			return _getTagsServletURL;
		}
		[Bindable("settingsChanged")]
		public function get taggingServletURL():String{
			return _taggingServletURL;
		}
		[Bindable("settingsChanged")]
		public function get tagCloudServletURL():String{
			return _tagCloudServletURL;
		}
		[Bindable("settingsChanged")]
		public function get getImageTagsServletURL():String{
			return _getImageTagsServletURL;
		}
		[Bindable("settingsChanged")]
		public function get logoURL():String{
			return _logoURL;	
		}
		
		public static function getInstance():Settings{
			if ( singleton == null )
				singleton = new Settings( arguments.callee );
			return singleton;
		}
		
		public function Settings( caller : Function = null ){ 
			if( caller != Settings.getInstance )
			throw new Error ("Singleton is a singleton class, use getInstance() instead");
			
			if ( Settings.singleton != null )
			throw new Error( "Only one Singleton instance should be instantiated" ); 
		}
		
		public function load():void{
			var httpService:HTTPService = new HTTPService();
			httpService.method = "GET";
			httpService.url = "settings/settings.xml";
			httpService.addEventListener(ResultEvent.RESULT, settingsLoaded);
			httpService.send();
		}
	
		private function settingsLoaded(event:ResultEvent):void{
			_qbeServletURL = event.result.settings.qbeservleturl.toString();
			_qbcServletURL = event.result.settings.querybycolorservleturl.toString();
			_randomServletURL = event.result.settings.randomservleturl.toString();
			_requestRSSServletURL = event.result.settings.requestrssservleturl.toString();
			_qbnServletURL = event.result.settings.qbnservleturl.toString();
			_qbtServletURL = event.result.settings.qbtservleturl.toString();
			_getTagsServletURL = event.result.settings.gettagsservleturl.toString();
			_taggingServletURL = event.result.settings.tagservleturl.toString();
			_tagCloudServletURL = event.result.settings.tagcloudservleturl.toString();
			_getImageTagsServletURL = event.result.settings.getimagetagsservleturl.toString();
			_logoURL = event.result.settings.logourl.toString();
			dispatchEvent(new Event("settingsChanged"));
		}
	}
}